﻿using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Tools;
using MyGameServer.Handler;
using Common;

namespace MyGameServer
{
    public class ClientPeer : Photon.SocketServer.ClientPeer
    {

        public float x;
        public float y;
        public float z;
        public string userName;
        public ClientPeer(InitRequest initRequest):base(initRequest) 
        {
        
        
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="reasonCode"></param>
        /// <param name="reasonDetail"></param>
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            MyGameServer.Instance.peerList.Remove(this);
        }
        /// <summary>
        /// 客户端发起请求
        /// </summary>
        /// <param name="operationRequest"></param>
        /// <param name="sendParameters"></param>
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            #region 没有家Code之前的代码
            //switch (operationRequest.OperationCode)
            //{
            //    case 0:
            //        break;
            //    case 1:
            //        Dictionary<byte, object> data = operationRequest.Parameters;
            //        object v3;
            //        data.TryGetValue(1, out v3);
            //        Object str;
            //        data.TryGetValue(2, out str);
            //        MyGameServer.log.Info("收到客户端发送一条消息得到的数据是：" + v3.ToString() + "------" + str.ToString());
            //        //给客户端一个响应
            //        OperationResponse opResponse = new OperationResponse(1);
            //        Dictionary<byte, object> _Temp = new Dictionary<byte, object>();
            //        _Temp.Add(1, 456456456);
            //        _Temp.Add(2, "45465");
            //        opResponse.SetParameters(_Temp);
            //        SendOperationResponse(opResponse, sendParameters);

            //        //
            //        EventData ed = new EventData(1);
            //        ed.SetParameters(_Temp);
            //        SendEvent(ed, new SendParameters());
            //        break;
            //    default:
            //        break;
            //}
            #endregion

            BaseHandler handler = DicTool.GetValue<OperationCode,BaseHandler>(MyGameServer.Instance.HandleDict ,(OperationCode)operationRequest.OperationCode);
            if (handler != null)
            {
                handler.OnOPerationRequest(operationRequest, sendParameters, this);
            }
            else 
            {
                BaseHandler defaultHandler = DicTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandleDict, OperationCode.Default);
                defaultHandler.OnOPerationRequest(operationRequest, sendParameters, this);
            }
        }
    }
}
